home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 029a / fcopyc10.zip / FCOPY.C < prev    next >
C/C++ Source or Header  |  1992-01-05  |  7KB  |  117 lines

  1. /*  FCOPY.C  --  contains _fcopy() function                             */
  2. /*                                                                      */
  3. /*  This function copies one file to another, much as the DOS COPY      */
  4. /*  command does.  It does not check to see if the two files are        */
  5. /*  the same, so it is up to the caller to verify that the two file     */
  6. /*  specs do not, in fact, refer to the same file.                      */
  7. /*                                                                      */
  8. /*  _fcopy() uses a farmalloc() call to allocate a large file buffer    */
  9. /*  from the far heap, regardless of the memory model being used.  In   */
  10. /*  order to perform reads and writes to this buffer, new _farread()    */
  11. /*  and _farwrite() functions were written.  The source code for these  */
  12. /*  functions is contained in FARREAD.ASM.                              */
  13. /*                                                                      */
  14. /*  BUFF_SIZE, indicating the buffer size, can be any int sized         */
  15. /*  value up to (but not including) 65,535.  It is recommended that     */
  16. /*  the buffer be a multiple of the sector size (512 bytes in the       */
  17. /*  current version of DOS), since reads and writes are more efficient  */
  18. /*  if they involve whole sectors.  A good value to use is 65024 (in    */
  19. /*  decimal), since this is 1 sector (512 bytes) short of a complete    */
  20. /*  segment (64k bytes).                                                */
  21. /*                                                                      */
  22. /*  Function prototypes are contained in fcopy.h                        */
  23. /*                                                                      */
  24. /*  Usage:                                                              */
  25. /*                                                                      */
  26. /*      #include "fcopy.h"                                              */
  27. /*      int _fcopy (char *sourcename, char *destname)                   */
  28. /*                                                                      */
  29. /*  The function returns 0 if sucessful, -1 otherwise.  Error codes     */
  30. /*  are returned in the global variables errno and _doserrno.           */
  31. /*  Possible error codes are:                                           */
  32. /*                                                                      */
  33. /*      EINVFNC     Invalid function number  (1)                        */
  34. /*      ENOFILE     File not found  (2)                                 */
  35. /*      ENOPATH     Path not found  (3)                                 */
  36. /*      EMFILE      Too many open files  (4)                            */
  37. /*      EACCESS     Permission denied  (5)                              */
  38. /*      EBADF       Bad file number  (6)                                */
  39. /*      ECONTR      Memory blocks destroyed  (7)                        */
  40. /*      ENOMEM      Not enough core  (8)                                */
  41. /*      EINVMEM     Invalid memory block address  (9)                   */
  42. /*      EINVACC     Invalid access code  (12)                           */
  43. /*           -1     Target disk full  (-1)                              */
  44. /*  ------------------------------------------------------------------- */
  45. /*  Revision history:                                                   */
  46. /*                                                                      */
  47. /*      1.0      5 JAN 92       Original.  Written for Turbo C++.       */
  48. /*  ------------------------------------------------------------------- */
  49. /*      Copyright (c) 1992 Ray Waters                                   */
  50. /*      All Rights Reserved                                             */
  51. /*  ------------------------------------------------------------------- */
  52.  
  53. #define BUFF_SIZE   65024U          /* size of file buffer */
  54.  
  55. #include <io.h>             /* _open, _creat, _close */
  56. #include <fcntl.h>          /* definition for O_RDONLY */
  57. #include <alloc.h>          /* farmalloc, farfree, NULL pointer */
  58. #include "fcopy.h"          /* _fcopy, _farread, _farwrite */
  59.  
  60. void __cleanup(void);       /* function to close files, free memory */
  61.  
  62. char far *buffer;
  63. int  SrcHandle, DestHandle;
  64.  
  65. int _fcopy(const char *sourcename, const char *destname)
  66. {
  67.     register int BytesRead;
  68.     struct ftime ft;
  69.  
  70.     SrcHandle = _open(sourcename, O_RDONLY);    /* open source (read only) */
  71.     if (SrcHandle == -1)                        /* if open failed, */
  72.         return -1;                              /* return error */
  73.     if (getftime(SrcHandle, &ft)) {             /* get date/time stamp */
  74.         _close(SrcHandle);                      /* if error, close file */
  75.         return -1;                              /* return error */
  76.     }
  77.  
  78.     DestHandle = _creat(destname, 0);           /* create/truncate target */
  79.     if (DestHandle == -1) {                     /* if open failed, */
  80.         _close(SrcHandle);                      /* close source file */
  81.         return -1;                              /* return error */
  82.     }
  83.  
  84.     if  (!(buffer = farmalloc(BUFF_SIZE))) {    /* allocate a far buffer */
  85.                                                 /* if allocation failed, */
  86.         _close(SrcHandle);                      /* close source file */
  87.         _close(DestHandle);                     /* close target file */
  88.         return -1;                              /* return error */
  89.     }
  90.  
  91.     do  {                                       /* read from source */
  92.         BytesRead = _farread(SrcHandle, buffer, BUFF_SIZE);
  93.         if (BytesRead == -1) {                  /* if read failed, */
  94.             __cleanup();                        /* close files, free mem */
  95.             return -1;                          /* return error */
  96.         }
  97.                                                 /* write to target */
  98.         if (_farwrite(DestHandle, buffer, BytesRead) == -1) {
  99.                                                 /* if write failed, */
  100.             __cleanup();                        /* close files, free mem */
  101.             return -1;                          /* return error */
  102.         }
  103.     }
  104.     while (BytesRead);                          /* loop until finished */
  105.  
  106.     setftime(DestHandle, &ft);              /* set target date/time stamp */
  107.     __cleanup();                                /* close files, free mem */
  108.     return 0;                                   /* return success */
  109. }
  110.  
  111. void __cleanup(void)                    /* close files and release memory */
  112. {
  113.     _close(SrcHandle);                          /* close source file */
  114.     _close(DestHandle);                         /* close target file */
  115.     farfree(buffer);                            /* free memory */
  116. }
  117.